package vn.edu.saigontech.ncms.model.info;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.OptimisticLockType;

import vn.edu.saigontech.ncms.model.BaseEntity;
import vn.edu.saigontech.ncms.model.enumeration.Campus;
import vn.edu.saigontech.ncms.model.enumeration.Gender;
import vn.edu.saigontech.ncms.model.enumeration.Nationality;
import vn.edu.saigontech.ncms.model.enumeration.TeacherType;
import vn.edu.saigontech.ncms.model.enumeration.Title;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true, optimisticLock = OptimisticLockType.ALL)
@Table(name = "ncms_teacher")
@SuppressWarnings("serial")
public class Teacher extends BaseEntity {

	@Column(name = "first_name", nullable = false)
	private String firstName;

	@Column(name = "last_name", nullable = false)
	private String lastName;

	@Column(name = "mid_name", nullable = true)
	private String midName;

	@Enumerated
	@Column(name = "gender")
	private Gender gender;
	
	@Enumerated
	@Column(name = "teacher_type")
	private TeacherType teacherType;

	@Enumerated
	@Column(name = "nationality")
	private Nationality nationality;

	@Column(name = "date_of_birth")
	@Temporal(TemporalType.DATE)
	private Date dateOfBirth;

	@Enumerated
	@Column(name = "campus")
	private Campus campus;

	@Column(name = "phone_number")
	private String phoneNumber;

	@Column(name = "email")
	private String email;

	@Column(name = "active")
	private boolean active;

	@Column(name = "comment", length = 4000)
	private String comment;

	public String getFullName() {
		return new StringBuilder(this.lastName).append(" ").append((this.midName != null?
				this.midName+" " : "")).append(this.firstName).toString();
	}
	
	public Title getTitle() {
		return (Gender.FEMALE.equals(this.gender)) ? Title.MS : Title.MR;
	}
	
	public boolean isSameName(String firstName, String lastName) {
		return this.firstName.equalsIgnoreCase(firstName) &&
			   this.lastName.equalsIgnoreCase(lastName);
 	}
	
	public boolean isDupplicate(String firstName, String lastName, Date dob) {
		return this.firstName.equalsIgnoreCase(firstName) &&
			   this.lastName.equalsIgnoreCase(lastName) &&
			   (this.dateOfBirth != null && this.dateOfBirth.equals(dob));
 	}
	
	public boolean isInstructor() {
		return TeacherType.INSTRUCTOR.equals(this.teacherType);
	}
	
	public boolean isTA() {
		return TeacherType.TA.equals(this.teacherType);
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMidName() {
		return midName;
	}

	public void setMidName(String midName) {
		this.midName = midName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public Nationality getNationality() {
		return nationality;
	}

	public void setNationality(Nationality nationality) {
		this.nationality = nationality;
	}

	public Campus getCampus() {
		return campus;
	}

	public void setCampus(Campus campus) {
		this.campus = campus;
	}

	public Gender getGender() {
		return gender;
	}

	public TeacherType getTeacherType() {
		return teacherType;
	}

	public void setTeacherType(TeacherType teacherType) {
		this.teacherType = teacherType;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	@Override
	public String toString() {
		return String.format("Teacher(%d, %s)", this.id, this.firstName);
	}
}